<template>
    <el-dialog
            title="我的评价"
            :visible.sync="dialogParams.visible"
            :close-on-click-modal="false"
            width="35%">
        <el-form ref="formData" :rules="rules" :model="formData" label-width="120px">
            <el-form-item label="评分" prop="assessFraction">
                <el-input
                        placeholder="请输入评分"
                        v-model.trim="formData.assessFraction">
                </el-input>
            </el-form-item>
            <el-form-item label="评价内容" prop="assessContent">
                <el-input
                        type="textarea"
                        :autosize="{ minRows: 4, maxRows: 8}"
                        placeholder="请输入评价内容"
                        v-model.trim="formData.assessContent">
                </el-input>
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button @click="close('formData')">关 闭</el-button>
            <el-button type="primary" @click="submit('formData')">保 存</el-button>
      </span>
    </el-dialog>
</template>

<script>
    import { addAssess } from '@/api/report';

    export default {
        name: 'addAssess',
        props: {
            dialogParams: {
                type: Object
            }
        },
        data () {
            const validateFraction = (rule, value, callback) => {
                if (value && !/^[0-9]+$/.test(value)) {
                    callback(new Error('评分必须为数字'));
                } else if (value > 100) {
                    callback(new Error('评分不能超过100'));
                } else {
                    callback();
                }
            };
            return {
                formData: {
                    assessFraction: '',
                    assessContent: ''
                },
                rules: {
                    assessFraction: [
                        { required: true, message: '请输入评分', trigger: 'blur' },
                        { validator: validateFraction }
                    ],
                    assessContent: [
                        { required: true, message: '请输入评价内容', trigger: 'blur' }
                    ]
                }
            }
        },
        methods: {
            submit (formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        const params = {
                            reportId: this.dialogParams.id,
                            assessFraction: +this.formData.assessFraction,
                            assessContent: this.formData.assessContent
                        };
                        addAssess(params).then(res => {
                            this.$message.success('操作成功');
                            this.dialogParams.visible = false;
                        }).catch(() => {})
                    } else {
                        return false;
                    }
                });
            },
            // 关闭
            close (formName) {
                this.$refs[formName].resetFields();
                this.dialogParams.visible = false;
            }
        }
    }
</script>

<style scoped>

</style>
